package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduCourseDao;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.edu.rep.EduCourseRep;
import com.stormrage.entity.learn.rep.LeaCourseStudyRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduCourseDaoImpl.java
 * @Package com.stormrage.edu.dao.edu.impl
 * @Description: 课程 Dao
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Repository
public class EduCourseDaoImpl extends MysBaseDao implements EduCourseDao {

	private static final Logger logger = LoggerFactory.getLogger(EduLecDaoImpl.class);

	@Override
	public EduCourse queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 QueryById 】-【 STARTED 】");

		final String query = "SELECT * FROM edu_course WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduCourse entity = queryObject(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public EduCourseRep queryRepById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepById 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course, a.detail, a.image, b.lec FROM edu_course AS a, edu_lec AS b"
				+ " WHERE a.lec_id = b.id AND a.id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduCourseRep entity = queryObject(query, EduCourseRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EduCourse> queryListByMajorId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryListByMajorId 】-【 STARTED 】");

		final String query = "SELECT a.* FROM edu_course AS a, edu_module_course AS b, edu_major_module AS c"
				+ " WHERE c.major_id = :majorId AND c.module_id = b.module_id AND b.course_id = a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourse> array = queryList(query, EduCourse.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<EduCourseRep> queryRepListByMajorId(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListByMajorId 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course, a.detail, a.image FROM edu_course AS a, edu_module_course AS b, edu_major_module AS c"
				+ " WHERE c.major_id = :majorId AND c.module_id = b.module_id AND b.course_id = a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourseRep> array = queryList(query, EduCourseRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaCourseStudyRep> queryStudyRepListByMajorId(final Map<String, Object> map)
			throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListByMajorId 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course, a.detail, a.image, d.lec FROM edu_course AS a,"
				+ " edu_module_course AS b, edu_major_module AS c, edu_lec AS d WHERE c.major_id = :majorId"
				+ " AND c.module_id = b.module_id AND b.course_id = a.id AND a.lec_id = d.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaCourseStudyRep> array = queryList(query, LeaCourseStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaCourseStudyRep> queryStudyRepList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryStudyRepList 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course, a.detail, a.image, c.id AS stuId, f.lec FROM edu_course AS a,"
				+ " lea_student_course AS b, lea_student AS c, lea_bunch AS d, lea_term AS e, edu_lec AS f"
				+ " WHERE a.id = b.course_id AND b.stu_id = c.id AND c.acct_id = :acctId AND c.bunch_id = d.id"
				+ " AND d.term_id = e.id AND NOW() > e.study_date_start AND NOW() < e.study_date_end"
				+ " AND e.major_id = :majorId AND a.lec_id = f.id AND b.ness = :ness";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaCourseStudyRep> array = queryList(query, LeaCourseStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryStudyRepList 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaCourseStudyRep> queryStudyRepListByTerm(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryStudyRepListByTerm 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course, a.detail, a.image, c.id AS stuId, f.lec FROM edu_course AS a,"
				+ " lea_student_course AS b, lea_student AS c, lea_bunch AS d, lea_term AS e, edu_lec AS f"
				+ " WHERE a.id = b.course_id AND b.stu_id = c.id AND c.acct_id = :acctId AND c.bunch_id = d.id"
				+ " AND d.term_id = :termId AND NOW() > e.study_date_start AND NOW() < e.study_date_end"
				+ " AND a.lec_id = f.id AND b.ness = :ness";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaCourseStudyRep> array = queryList(query, LeaCourseStudyRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryStudyRepListByTerm 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<EduCourseRep> queryRepListForSelect(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListForSelect 】-【 STARTED 】");

		final String query = "SELECT a.id, a.course FROM edu_course AS a, edu_module_course AS b,"
				+ " edu_major_module AS c, edu_module AS d WHERE c.major_id = :majorId AND c.module_id = b.module_id"
				+ " AND b.course_id = a.id AND c.module_id = d.id AND d.ness = 0";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduCourseRep> array = queryList(query, EduCourseRep.class, map);

		logger.debug(" 【 Dao 】-【 EduCourseDao 】-【 queryRepListForSelect 】-【 COMPLETED 】");
		return array;

	}

}
